package cc.lj.internal.coupons.domain.ticket.service.impl;

import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.core.model.PageBean;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.coupons.domain.ticket.model.vo.CouponsSettlementDO;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketBatchDO;
import cc.lj.internal.coupons.domain.ticket.model.vo.convert.TicketBatchPOConvertDO;
import cc.lj.internal.coupons.domain.ticket.repository.ITicketBatchRepository;
import cc.lj.internal.coupons.domain.ticket.service.ITicketBatchDomainService;
import cc.lj.internal.coupons.infrastructure.excepion.CoupousExceptionEnum;
import cc.lj.internal.coupons.infrastructure.po.TicketBatchLifecycleContinuePO;
import cc.lj.internal.coupons.infrastructure.po.TicketBatchPO;
import cc.lj.internal.coupons.infrastructure.repository.TicketBatchLifecycleContinueRespository;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 电子券批次表领域服务service实现类
 * @author liangxifeng
 * @date 2021-12-14
 */
@Service("ticketBatchDomainServiceImpl")
@Slf4j
public class TicketBatchDomainServiceImpl implements ITicketBatchDomainService {
    //电子券批次表仓储接口
    @Resource(name = "ticketBatchRepository")
    private ITicketBatchRepository repository;

    //电子券批次连续有效期repository
    @Resource(name = "ticketBatchLifecycleContinueRespository")
    private TicketBatchLifecycleContinueRespository continueRespository;

    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @Override
    public PageBean<TicketBatchDO> selectPage(PageParam<TicketBatchDO> pageParam) {
        PageInfo<TicketBatchPO> pageInfo = repository.selectPage(pageConvert(pageParam));

        List<TicketBatchDO> list = new ArrayList();
        if(pageInfo.getList().size() > 0)
        {
            for (TicketBatchPO TicketBatchPO : pageInfo.getList()) {
                TicketBatchDO activityDO = TicketBatchPOConvertDO.INSTANCE.convert(TicketBatchPO);
                list.add(activityDO);
            }
        }
        //list的po转换为do后再组装PageBean将其返回上一层
        PageBean pageBean = PageBean.createPageBean(pageInfo.getPageNum(),
                pageInfo.getPageSize(), pageInfo.getTotal(), list);
        return pageBean;
    }


    /**
     * 分页DO to PO
     * @param pageParam
     * @return
     */
    public PageParam<TicketBatchPO> pageConvert(PageParam<TicketBatchDO> pageParam) {
        PageParam<TicketBatchPO> pageParamR = new PageParam<TicketBatchPO>();
        pageParamR.setPageNum( pageParam.getPageNum() ) ;
        pageParamR.setPageSize( pageParam.getPageSize() );
        pageParamR.setOrderBy( pageParam.getOrderBy() );
        TicketBatchPO activityPO = TicketBatchPOConvertDO.INSTANCE.convert(pageParam.getParam());
        pageParamR.setParam(activityPO);
        return pageParamR;
    }

    /**
     * 通过主键查询一条记录
     * @param id
     * @return
     */
    public TicketBatchDO getById(Integer id)
    {
        TicketBatchPO TicketBatchPO = repository.getById(id);
        return TicketBatchPOConvertDO.INSTANCE.convert(TicketBatchPO);
    }

    /**
     * 根据id修改
     * @param ticketBatchDO
     * @return
     */
    public Integer updateById(TicketBatchDO ticketBatchDO) {
        //查询活动批次
        TicketBatchPO batchPO = repository.getById(ticketBatchDO.getBatchId());
        TicketBatchPO TicketBatchPO = TicketBatchPOConvertDO.INSTANCE.convert(ticketBatchDO);
        return repository.updateById(TicketBatchPO);
    }

    @Override
    public Integer bindActivityCode(Integer batchId, String activityCode) {
        //验证该批次已被其他活动关联
        TicketBatchPO ticketBatchPO = repository.getById(batchId);
        log.info("ticketBatchPO = "+ticketBatchPO);
        if(ticketBatchPO.getActivityCode() != null && !ticketBatchPO.getActivityCode().isEmpty() ) {
            log.info("该批次batchId="+batchId+",已被其他活动关联,绑定的活动编号="+ticketBatchPO.getActivityCode());
            throw new ServiceException(CoupousExceptionEnum.TIKCT_BATH_BINDED);
        }

        //验证该活动编号是否被其他批次绑定
        TicketBatchPO queryPO = new TicketBatchPO();
        queryPO.setActivityCode(activityCode);
        List<TicketBatchPO> batchPOList = repository.getList(queryPO);
        if(batchPOList.size() > 0) {
            log.info("该活动编号activityCode="+activityCode+",已被其他批次绑定，已关联的批次list="+batchPOList);
            throw new ServiceException(CoupousExceptionEnum.ACTIVITY_CODE_BINDED);
        }

        //绑定批次与活动编号
        TicketBatchPO addBatchPO = new TicketBatchPO();
        addBatchPO.setBatchId(batchId);
        addBatchPO.setActivityCode(activityCode.trim());
        Integer rows = repository.updateById(addBatchPO);
        return rows;
    }

    /**
     * 标准批量获取数据的方法
     * @param ticketBatchDO
     * @return
     */
    public List<TicketBatchDO> getList(TicketBatchDO ticketBatchDO)
    {
        TicketBatchPO ticketBatchPO = TicketBatchPOConvertDO.INSTANCE.convert(ticketBatchDO);
        List<TicketBatchPO> list = repository.getList(ticketBatchPO);
        List<TicketBatchDO> newList = new ArrayList<>();
        for(TicketBatchPO item : list)
        {
            newList.add(TicketBatchPOConvertDO.INSTANCE.convert(item));
        }
        return newList;
    }

    @Override
    public Integer unBindActivityCode(Integer batchId) {
        TicketBatchDO ticketBatchDO = new TicketBatchDO();
        ticketBatchDO.setBatchId(batchId);
        //修改活动编号为空
        ticketBatchDO.setActivityCode("");
        TicketBatchPO ticketBatchPO = TicketBatchPOConvertDO.INSTANCE.convert(ticketBatchDO);
        return repository.updateById(ticketBatchPO);
    }

    @Override
    public List<TicketBatchDO> getNoStartList() {
        //先查询连续有效期表未开始的数据，且拼接批次主键id list
        List<TicketBatchLifecycleContinuePO> listPO = continueRespository.getNoStartList();
        if(listPO.size()==0) {
           return new ArrayList();
        }
        List<Integer> batchIdList = new ArrayList<>();
        for (TicketBatchLifecycleContinuePO ticketBatchLifecycleContinuePO : listPO) {
            batchIdList.add(ticketBatchLifecycleContinuePO.getLifeBatchId());
        }
        //通过主键进行批量查找批次数据
        List<TicketBatchPO> batchPOList = repository.getNoBindCodeListByIds(batchIdList);
        List<TicketBatchDO> batchDOList = new ArrayList<>();
        for (TicketBatchPO ticketBatchPO : batchPOList) {
            batchDOList.add(TicketBatchPOConvertDO.INSTANCE.convert(ticketBatchPO));
        }
        return batchDOList;
    }

    @Override
    public List<TicketBatchDO> getListByIds(List<Integer> batchIds) {
        List<TicketBatchPO> listPO = repository.getByIds(batchIds);
        List<TicketBatchDO> listDO = new ArrayList<>();
        for (TicketBatchPO ticketBatchPO : listPO) {
            if(ticketBatchPO.getBatchTypeId().compareTo(new BigDecimal(2))==0) {
                listDO.add(TicketBatchPOConvertDO.INSTANCE.convert(ticketBatchPO));
            }
        }
        return listDO;
    }

    @Override
    public CouponsSettlementDO createOldShare(Integer batchId) {
        //通过主键查询电子券批次表数据
        TicketBatchPO batchPO = repository.getById(batchId);
        /**
         * 组合原始分摊数据
         */
        CouponsSettlementDO settlementDO = new CouponsSettlementDO();
        //原商户分摊金额
        settlementDO.setMerchantOldSharePrice(batchPO.getMerchantSharePrice());

        //增值金额 = 券面额-售券金额
        BigDecimal zengZhi = batchPO.getBatchPrice().subtract(batchPO.getSellPrice());
        //原卖场分摊金额 = 增值金额-商户分摊
        settlementDO.setMartOldSharePrice(zengZhi.subtract(batchPO.getMerchantSharePrice()));
        //电子券批次主键
        settlementDO.setTicketBatchId(batchPO.getBatchId());
        //原卖场分摊比
        settlementDO.setMartOldShareRatio(batchPO.getBatchStoreShare());
        //原商户分摊比
        settlementDO.setMerchantOldShareRatio(batchPO.getBatchMerchantShare());
        //电子券售券金额
        settlementDO.setSellPrice(batchPO.getSellPrice());
        return settlementDO;
    }

}
